package com.hupu.jrs.ui.pmlist

import com.hupu.jrs.api.game.GameApi
import com.hupu.jrs.bean.Pm
import com.hupu.jrs.injector.PerActivity
import com.hupu.jrs.util.ToastUtil
import java.util.ArrayList
import javax.inject.Inject
import rx.Subscription
import rx.android.schedulers.AndroidSchedulers
import rx.functions.Action1
import rx.schedulers.Schedulers

/**
 * Created by sll on 2016/5/11.
 */
@PerActivity
class PmListPresenter @Inject
constructor(private val mGameApi: GameApi) : PmListContract.Presenter {

    private var mPmListView: PmListContract.View? = null
    private var lastTime = ""
    private val mPms = ArrayList<Pm>()
    private var hasNextPage = true
    private var mSubscription: Subscription? = null

    override fun onPmListReceive() {
        mPmListView!!.showLoading()
        loadPmList(true)
    }

    private fun loadPmList(clear: Boolean) {
        mSubscription = mGameApi.queryPmList(lastTime).subscribeOn(Schedulers.io()).doOnNext(Action1<Any> {
            if (clear) {
                mPms.clear()
            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribe(Action1<Any> { pmData ->
            mPmListView!!.hideLoading()
            if (pmData != null) {
                val result = pmData.result
                hasNextPage = result.has_next_page.equals("1")
                if (result.data.isEmpty()) {
                    if (mPms.isEmpty()) {
                        mPmListView!!.onEmpty()
                    } else {
                        ToastUtil.showToast("没有更多了")
                    }
                } else {
                    addPmList(result.data)
                    mPmListView!!.renderPmList(mPms)
                }
                mPmListView!!.onRefreshCompleted()
                mPmListView!!.onLoadCompleted(hasNextPage)
            }
        }, Action1<Throwable> {
            if (mPms.isEmpty()) {
                mPmListView!!.onError()
            } else {
                ToastUtil.showToast("数据加载失败，请检查网络后重试")
                mPmListView!!.hideLoading()
                mPmListView!!.onRefreshCompleted()
                mPmListView!!.onLoadCompleted(true)
            }
        })
    }

    private fun addPmList(pms: List<Pm>) {
        for (pm in pms) {
            var contain = false
            for (mPm in mPms) {
                if (pm.sid.equals(mPm.sid)) {
                    contain = true
                    break
                }
            }
            if (!contain) {
                mPms.add(pm)
            }
        }
        if (!mPms.isEmpty()) {
            lastTime = mPms[mPms.size - 1].last_time
        }
    }

    override fun onRefresh() {
        lastTime = ""
        loadPmList(true)
    }

    override fun onReload() {
        onPmListReceive()
    }

    override fun onLoadMore() {
        if (!hasNextPage) {
            ToastUtil.showToast("没有更多了~")
            mPmListView!!.onLoadCompleted(false)
            return
        }
        loadPmList(false)
    }

    override fun onPmListClick(pm: Pm) {
        mPmListView!!.showPmDetailUi(pm.puid.toString(), pm.nickname.toString())
    }

    override fun detachView() {
        if (mSubscription != null && !mSubscription!!.isUnsubscribed) {
            mSubscription!!.unsubscribe()
        }
        mPmListView = null
    }

    override fun attachView(view: PmListContract.View) {
        mPmListView = view
    }
}
